package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielConfirmBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMaterielConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrCommitActivity extends BaseActivity {
    private TextView title, submitLine, operator;
    private TextView confirm;
    private PercentRelativeLayout secondBlock;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_matr_commit);
        initView(this);
        initData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        operator = (TextView) activity.findViewById(R.id.operator);
        secondBlock = (PercentRelativeLayout) activity.findViewById(R.id.secondBlock);
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                StorageManageMaterielConfirmRequestBean params = StorageManageUtils.getStorageManageMaterielConfirmRequestBean(activity);
                StorageManageMaterielConfirmRequest.request(activity, params, new GsonBeanListener<StorageManageMaterielConfirmBean>() {
                    @Override
                    public void onSuccess(StorageManageMaterielConfirmBean bean) {
                        if (bean.getHead().getStatus().equals(RequestConstant.STATUS_SUCCESS)) {
                            SPUtils.removeStorageMaterielInformation(activity);
                            Intent intent = new Intent(activity, StorageManageResultActivity.class);
                            startActivity(intent);
                        } else {
                            if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                                Utils.middleToast(activity, bean.getHead().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailing(StorageManageMaterielConfirmBean bean) {
                        if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                            Utils.middleToast(activity, bean.getHead().getMsg());
                        }
                    }
                });
            }
        });
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_commit_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_up_frame_commit_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void getData(Context context) {
        List<StorageManageMatrBean.BodyBean> materielList = StorageManageUtils.getStorageMaterielBean(context).getMaterielList();
        operator.setText(SPUtils.getName(this));
        submitLine.setText(StorageManageUtils.getStorageMaterielConfirmNumber(context) + "");
        secondBlock.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, StorageManageUtils.getOverViewBeanWithStorageMaterielList(materielList)).show();
            }
        });
    }

}
